<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户ID" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户ID"></el-input>
    </el-form-item>
    <el-form-item label="消息类型(1平台公共消息,2指定用户消息)" prop="type">
      <el-input v-model="dataForm.type" placeholder="消息类型(1平台公共消息,2指定用户消息)"></el-input>
    </el-form-item>
    <el-form-item label="消息标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="消息标题"></el-input>
    </el-form-item>
    <el-form-item label="消息内容，富文本" prop="content">
      <el-input v-model="dataForm.content" placeholder="消息内容，富文本"></el-input>
    </el-form-item>
    <el-form-item label="是否已读(0未读,1已读)" prop="isReaded">
      <el-input v-model="dataForm.isReaded" placeholder="是否已读(0未读,1已读)"></el-input>
    </el-form-item>
    <el-form-item label="业务状态(1启用,2禁用)" prop="status">
      <el-input v-model="dataForm.status" placeholder="业务状态(1启用,2禁用)"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="createBy">
      <el-input v-model="dataForm.createBy" placeholder="创建人"></el-input>
    </el-form-item>
    <el-form-item label="修改人" prop="updateBy">
      <el-input v-model="dataForm.updateBy" placeholder="修改人"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createDate">
      <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateDate">
      <el-input v-model="dataForm.updateDate" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="乐观锁" prop="version">
      <el-input v-model="dataForm.version" placeholder="乐观锁"></el-input>
    </el-form-item>
    <el-form-item label="是否删除" prop="isDelete">
      <el-input v-model="dataForm.isDelete" placeholder="是否删除"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          type: '',
          title: '',
          content: '',
          isReaded: '',
          status: '',
          createBy: '',
          updateBy: '',
          createDate: '',
          updateDate: '',
          version: '',
          isDelete: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户ID不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '消息类型(1平台公共消息,2指定用户消息)不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '消息标题不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '消息内容，富文本不能为空', trigger: 'blur' }
          ],
          isReaded: [
            { required: true, message: '是否已读(0未读,1已读)不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '业务状态(1启用,2禁用)不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          updateBy: [
            { required: true, message: '修改人不能为空', trigger: 'blur' }
          ],
          createDate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateDate: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '乐观锁不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '是否删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/message/message/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.message.userId
                this.dataForm.type = data.message.type
                this.dataForm.title = data.message.title
                this.dataForm.content = data.message.content
                this.dataForm.isReaded = data.message.isReaded
                this.dataForm.status = data.message.status
                this.dataForm.createBy = data.message.createBy
                this.dataForm.updateBy = data.message.updateBy
                this.dataForm.createDate = data.message.createDate
                this.dataForm.updateDate = data.message.updateDate
                this.dataForm.version = data.message.version
                this.dataForm.isDelete = data.message.isDelete
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/message/message/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'type': this.dataForm.type,
                'title': this.dataForm.title,
                'content': this.dataForm.content,
                'isReaded': this.dataForm.isReaded,
                'status': this.dataForm.status,
                'createBy': this.dataForm.createBy,
                'updateBy': this.dataForm.updateBy,
                'createDate': this.dataForm.createDate,
                'updateDate': this.dataForm.updateDate,
                'version': this.dataForm.version,
                'isDelete': this.dataForm.isDelete
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
